package com.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.org.mapper.CollectMapper;
import com.org.mapper.CommunityAdminMapper;
import com.org.pojo.Collect;
import com.org.pojo.CommunityAdmin;
import com.org.common.PageBean;
import com.org.service.CommunityAdminService;
import com.org.utils.Md5Util;
import com.org.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CommunityUserServiceImpl
 * Package: com.org.service.impl
 * Description:
 *
 * @Author Origin
 * @Create 2024/2/3 0:04
 * @Version 1.0
 */
@Service
public class CommunityAdminServiceImpl extends ServiceImpl<CommunityAdminMapper, CommunityAdmin> implements CommunityAdminService {

    @Autowired
    private CommunityAdminMapper communityAdminMapper;

    @Override
    public CommunityAdmin findByUsername(String username) {
        CommunityAdmin c = communityAdminMapper.findByUsername(username);
        return c;
    }

    @Override
    public void update(CommunityAdmin communityAdmin) {
        communityAdmin.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        communityAdmin.setId(id);
//        System.out.println(communityAdmin);
        communityAdminMapper.update(communityAdmin);
    }

    @Override
    public void updateState(String state, String username) {

        communityAdminMapper.updateState(state, username);
    }

    @Override
    public void updateCommunityId(Integer communityId, Integer communityAdminId) {
        communityAdminMapper.updateCommunityId(communityId, communityAdminId);
    }

    @Override
    public void updateAvatar(String url) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        communityAdminMapper.updateAvatar(url, id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        newPwd = Md5Util.getMD5String(newPwd);
        communityAdminMapper.updatePwd(newPwd, id);
    }

    @Override
    public PageBean<CommunityAdmin> list(
            Integer pageNum,
            Integer pageSize,
            String username,
            String state) {
        //1. 创建PageBean对象
        PageBean<CommunityAdmin> pb = new PageBean<>();

        //2.开启分页查询
        PageHelper.startPage(pageNum, pageSize);

        //3.调用mapper
        QueryWrapper<CommunityAdmin> ql = new QueryWrapper<>();
        ql.like("username", username)
                .like("state", state);

        List<CommunityAdmin> as = communityAdminMapper.selectList(ql);

        //Page中提供了方法，可以获取PageHelper分页查询后 得到的总记录条数和当页数据
        Page<CommunityAdmin> p = (Page<CommunityAdmin>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());  //总共多少页
        pb.setItems(p.getResult()); //当前页内容
        return pb;
    }

    @Override
    public void delete(Integer id) {
        communityAdminMapper.deleteById(id);
    }

    @Override
    public CommunityAdmin getInfoById(Integer id) {
        return communityAdminMapper.selectById(id);
    }
}
